package com.itstyle.jpa.service.impl;

import java.text.SimpleDateFormat;
import java.util.*;
import java.util.concurrent.CountDownLatch;
import java.util.stream.Collectors;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.itstyle.jpa.entity.PointChangeLog;
import com.itstyle.jpa.model.*;
import com.itstyle.jpa.repository.H5CounterRpository;
import com.itstyle.jpa.repository.OrganizationRepository;
import com.itstyle.jpa.utils.QRCodeUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.namedparam.NamedParameterJdbcTemplate;
import org.springframework.jdbc.core.namedparam.SqlParameterSource;
import org.springframework.jdbc.core.namedparam.SqlParameterSourceUtils;
import org.springframework.stereotype.Service;

import com.itstyle.jpa.dynamicquery.DynamicQuery;
import com.itstyle.jpa.service.IStudentService;

@Service
public class StudentServiceImpl implements IStudentService {
	
	final static SimpleDateFormat sdf = new SimpleDateFormat(
			"yyyy-MM-dd HH:mm:ss");
	
	final static String startTime = sdf.format(new Date());
	@Autowired
	private DynamicQuery dynamicQuery;
	@Autowired
	private NamedParameterJdbcTemplate namedParameterJdbcTemplate;//批量插入
	@Autowired
	H5CounterRpository h5CounterRpository;
	@Autowired
	OrganizationRepository organizationRepository;
	
	@Override
	public List<Object[]> listStudent() {
		String nativeSql = "SELECT s.id AS studentId,c.id AS classId,c.class_name AS className,c.teacher_name AS teacherName,s.name,s.age FROM app_student s,app_class c";
		List<Object[]> list = dynamicQuery.nativeQueryList(nativeSql, new Object[]{});
		return list;
	}
	
	@Override
	public List<Student> listStudentModel() {
		String nativeSql = "SELECT s.id AS studentId,c.id AS classId,c.class_name AS className,c.teacher_name AS teacherName,s.name,s.age FROM app_student s,app_class c";
		List<Student> list = dynamicQuery.nativeQueryListModel(Student.class, nativeSql, new Object[]{});
		return list;
	}

	@Override
	public void UpdateH5Counter() {

		List<H5Counter> all =  h5CounterRpository.findAll();

		for (H5Counter h5Counter : all) {
			String url="https://wxmini.yuesai.com/H5/#/auth?batch=1&counterCode="+h5Counter.getCounterCode();

			h5Counter.setQrUrl(url);
		}

		h5CounterRpository.save(all);



	}

	@Override
	public void uploadQrcode() {

		List<H5Counter> all =  h5CounterRpository.findAll();

		for (H5Counter h5Counter : all) {


			String filePath = "D:/qrcode/"+h5Counter.getCounterName()+".png";

			QRCodeUtil.encodeQRCode(h5Counter.getQrUrl(),filePath);

		}

	}

	@Override
	public List<Long> digui() {

		List<Organization> byParentOrgId =  organizationRepository.findByParentOrgId(3);

		List<Long> result=new ArrayList<>();

		dg(result,byParentOrgId);

		return result;
	}


	public void dg( List<Long> res, List<Organization> citys) {
		List<Organization> retList = new ArrayList<>();

		for (Organization c : citys) {
			retList = organizationRepository.findByParentOrgId(c.getParentOrgId());
			if (retList.size() > 0) {

				for (Organization organization : retList) {
					res.add(organization.getId());
				}

				dg(res,retList); //循环调用自己
			}
		}
	}


	@Override
	public List<Map<Object,Object>> listStudentMap() {
		String nativeSql = "SELECT s.id AS studentId,c.id AS classId,c.class_name AS className,c.teacher_name AS teacherName,s.name,s.age FROM app_student s,app_class c";
		List<Map<Object,Object>> list = dynamicQuery.nativeQueryListMap(nativeSql, new Object[]{});
		return list;
	}

	@Override
	public void batchSave() {
		List<AppStudent> list = new ArrayList<AppStudent>();
		list.add(new AppStudent(1,"张三",21));
		list.add(new AppStudent(1,"李四",22));
		list.add(new AppStudent(1,"王二麻子",23));
		SqlParameterSource[] beanSources  = SqlParameterSourceUtils.createBatch(list.toArray());
		String sql = "INSERT INTO app_student(class_id,name,age) VALUES (:classId,:name,:age) ";
		namedParameterJdbcTemplate.batchUpdate(sql, beanSources);
	}

	@Override
	public void statsDemo() throws Exception {
		CountDownLatch latch = new CountDownLatch(5);// 两个赛跑者
		Stats stats1 = new Stats("任务A", 1000, latch);
		Stats stats2 = new Stats("任务B", 2000, latch);
		Stats stats3 = new Stats("任务C", 2000, latch);
		Stats stats4 = new Stats("任务D", 2000, latch);
		Stats stats5 = new Stats("任务E", 2000, latch);
		stats1.start();//任务A开始执行
		stats2.start();//任务B开始执行
		stats3.start();//任务C开始执行
		stats4.start();//任务D开始执行
		stats5.start();//任务E开始执行
		latch.await();// 等待所有人任务结束
		System.out.println("所有的统计任务执行完成:" + sdf.format(new Date()));
	}
	static class Stats extends Thread {
		String statsName;
		int runTime;
		CountDownLatch latch;

		public Stats(String statsName, int runTime, CountDownLatch latch) {
			this.statsName = statsName;
			this.runTime = runTime;
			this.latch = latch;
		}

		public void run() {
			try {
				System.out.println(statsName+ " do stats begin at "+ startTime);
				//模拟任务执行时间
				Thread.sleep(runTime);
				System.out.println(statsName + " do stats complete at "+ sdf.format(new Date()));
				latch.countDown();//单次任务结束，计数器减一
			} catch (InterruptedException e) {
				e.printStackTrace();
			}
		}
	}


//	public static void main(String[] args) {
//
//
//		String x="[{\"accrualedValue\":100,\"date\":\"08/12/2014 09:08:00\",\"mainBody\":1,\"point\":\"+100积分\",\"sortDate\":1407772800000,\"type\":\"积分调整\",\"usedValue\":100},{\"accrualedValue\":19,\"date\":\"06/08/2015 02:26:19\",\"mainBody\":1,\"point\":\"+19积分\",\"sortDate\":1433692800000,\"type\":\"消费积分\",\"usedValue\":19},{\"date\":\"11/03/2018 02:22:34\",\"point\":\"-19积分\",\"sortDate\":1541174400000,\"type\":\"积分到期\"},{\"accrualedValue\":390,\"date\":\"06/08/2015 02:26:19\",\"mainBody\":1,\"point\":\"+390积分\",\"sortDate\":1433692800000,\"type\":\"消费积分\",\"usedValue\":390},{\"date\":\"11/03/2018 02:22:34\",\"point\":\"-390积分\",\"sortDate\":1541174400000,\"type\":\"积分到期\"},{\"accrualedValue\":390,\"date\":\"06/08/2015 02:26:19\",\"mainBody\":1,\"point\":\"+390积分\",\"sortDate\":1433692800000,\"type\":\"消费积分\",\"usedValue\":390},{\"date\":\"11/03/2018 02:22:34\",\"point\":\"-390积分\",\"sortDate\":1541174400000,\"type\":\"积分到期\"},{\"accrualedValue\":410,\"date\":\"06/08/2015 02:26:19\",\"mainBody\":1,\"point\":\"+410积分\",\"sortDate\":1433692800000,\"type\":\"消费积分\",\"usedValue\":410},{\"date\":\"11/03/2018 02:22:34\",\"point\":\"-410积分\",\"sortDate\":1541174400000,\"type\":\"积分到期\"},{\"accrualedValue\":180,\"date\":\"10/05/2015 03:05:43\",\"mainBody\":1,\"point\":\"+180积分\",\"sortDate\":1443974400000,\"type\":\"消费积分\",\"usedValue\":180},{\"date\":\"11/03/2018 02:22:34\",\"point\":\"-180积分\",\"sortDate\":1541174400000,\"type\":\"积分到期\"},{\"accrualedValue\":260,\"date\":\"10/05/2015 03:05:43\",\"mainBody\":1,\"point\":\"+260积分\",\"sortDate\":1443974400000,\"type\":\"消费积分\",\"usedValue\":260},{\"date\":\"11/03/2018 02:22:34\",\"point\":\"-260积分\",\"sortDate\":1541174400000,\"type\":\"积分到期\"},{\"accrualedValue\":390,\"date\":\"10/05/2015 03:05:43\",\"mainBody\":1,\"point\":\"+390积分\",\"sortDate\":1443974400000,\"type\":\"消费积分\",\"usedValue\":390},{\"date\":\"11/03/2018 02:22:34\",\"point\":\"-390积分\",\"sortDate\":1541174400000,\"type\":\"积分到期\"},{\"accrualedValue\":540,\"date\":\"01/22/2016 02:47:40\",\"mainBody\":1,\"point\":\"+540积分\",\"sortDate\":1453392000000,\"type\":\"升级奖励积分\",\"usedValue\":540},{\"date\":\"08/03/2016 02:25:52\",\"point\":\"-540积分\",\"sortDate\":1470153600000,\"type\":\"积分到期\"},{\"accrualedValue\":540,\"date\":\"01/29/2016 07:34:41\",\"mainBody\":1,\"point\":\"+540积分\",\"sortDate\":1453996800000,\"type\":\"积分调整\",\"usedValue\":540},{\"date\":\"08/03/2016 02:25:52\",\"point\":\"-540积分\",\"sortDate\":1470153600000,\"type\":\"积分到期\"},{\"accrualedValue\":150,\"date\":\"02/02/2016 03:07:09\",\"mainBody\":1,\"point\":\"+150积分\",\"sortDate\":1454342400000,\"type\":\"消费积分\",\"usedValue\":150},{\"date\":\"11/03/2018 02:22:34\",\"point\":\"-150积分\",\"sortDate\":1541174400000,\"type\":\"积分到期\"},{\"accrualedValue\":165,\"date\":\"07/17/2016 04:09:41\",\"mainBody\":1,\"point\":\"+165积分\",\"sortDate\":1468684800000,\"type\":\"消费积分\",\"usedValue\":165},{\"date\":\"11/03/2018 02:22:34\",\"point\":\"-165积分\",\"sortDate\":1541174400000,\"type\":\"积分到期\"},{\"accrualedValue\":165,\"date\":\"07/17/2016 04:09:41\",\"mainBody\":1,\"point\":\"+165积分\",\"sortDate\":1468684800000,\"type\":\"消费积分\",\"usedValue\":165},{\"date\":\"11/03/2018 02:22:34\",\"point\":\"-165积分\",\"sortDate\":1541174400000,\"type\":\"积分到期\"},{\"accrualedValue\":270,\"date\":\"07/17/2016 04:09:41\",\"mainBody\":1,\"point\":\"+270积分\",\"sortDate\":1468684800000,\"type\":\"消费积分\",\"usedValue\":270},{\"date\":\"11/03/2018 02:22:34\",\"point\":\"-270积分\",\"sortDate\":1541174400000,\"type\":\"积分到期\"},{\"accrualedValue\":310,\"date\":\"07/17/2016 04:09:41\",\"mainBody\":1,\"point\":\"+310积分\",\"sortDate\":1468684800000,\"type\":\"消费积分\",\"usedValue\":310},{\"date\":\"11/03/2018 02:22:34\",\"point\":\"-310积分\",\"sortDate\":1541174400000,\"type\":\"积分到期\"},{\"accrualedValue\":410,\"date\":\"07/17/2016 04:09:41\",\"mainBody\":1,\"point\":\"+410积分\",\"sortDate\":1468684800000,\"type\":\"消费积分\",\"usedValue\":410},{\"date\":\"11/03/2018 02:22:34\",\"point\":\"-410积分\",\"sortDate\":1541174400000,\"type\":\"积分到期\"},{\"accrualedValue\":140,\"date\":\"04/20/2017 03:54:16\",\"mainBody\":1,\"point\":\"+140积分\",\"sortDate\":1492617600000,\"type\":\"消费积分\",\"usedValue\":140},{\"date\":\"11/03/2018 02:22:34\",\"point\":\"-140积分\",\"sortDate\":1541174400000,\"type\":\"积分到期\"},{\"accrualedValue\":150,\"date\":\"04/20/2017 03:54:16\",\"mainBody\":1,\"point\":\"+150积分\",\"sortDate\":1492617600000,\"type\":\"消费积分\",\"usedValue\":150},{\"date\":\"11/03/2018 02:22:34\",\"point\":\"-150积分\",\"sortDate\":1541174400000,\"type\":\"积分到期\"},{\"accrualedValue\":165,\"date\":\"09/10/2017 22:12:53\",\"mainBody\":1,\"point\":\"+165积分\",\"sortDate\":1504972800000,\"type\":\"消费积分\",\"usedValue\":165},{\"date\":\"11/03/2018 02:22:34\",\"point\":\"-165积分\",\"sortDate\":1541174400000,\"type\":\"积分到期\"},{\"accrualedValue\":330,\"date\":\"09/10/2017 22:12:52\",\"mainBody\":1,\"point\":\"+330积分\",\"sortDate\":1504972800000,\"type\":\"消费积分\",\"usedValue\":330},{\"date\":\"11/03/2018 02:22:34\",\"point\":\"-330积分\",\"sortDate\":1541174400000,\"type\":\"积分到期\"},{\"accrualedValue\":330,\"date\":\"09/10/2017 22:12:53\",\"mainBody\":1,\"point\":\"+330积分\",\"sortDate\":1504972800000,\"type\":\"消费积分\",\"usedValue\":330},{\"date\":\"11/03/2018 02:22:34\",\"point\":\"-330积分\",\"sortDate\":1541174400000,\"type\":\"积分到期\"},{\"accrualedValue\":410,\"date\":\"09/10/2017 22:12:53\",\"mainBody\":1,\"point\":\"+410积分\",\"sortDate\":1504972800000,\"type\":\"消费积分\",\"usedValue\":410},{\"date\":\"11/03/2018 02:22:34\",\"point\":\"-410积分\",\"sortDate\":1541174400000,\"type\":\"积分到期\"},{\"accrualedValue\":90,\"date\":\"08/01/2019 09:13:12\",\"mainBody\":1,\"point\":\"+90积分\",\"sortDate\":1564588800000,\"type\":\"积分调整\",\"usedValue\":90},{\"date\":\"02/19/2020 00:29:37\",\"point\":\"-78积分\",\"sortDate\":1582041600000,\"type\":\"退单\"},{\"date\":\"10/29/2019 08:23:07\",\"point\":\"-1积分\",\"sortDate\":1572278400000,\"type\":\"积分兑礼\"},{\"date\":\"10/29/2019 08:52:42\",\"point\":\"-1积分\",\"sortDate\":1572278400000,\"type\":\"积分兑礼\"},{\"date\":\"10/29/2019 08:56:56\",\"point\":\"-1积分\",\"sortDate\":1572278400000,\"type\":\"积分兑礼\"},{\"date\":\"10/29/2019 13:26:10\",\"point\":\"-1积分\",\"sortDate\":1572278400000,\"type\":\"积分兑礼\"},{\"date\":\"10/29/2019 08:50:14\",\"point\":\"-1积分\",\"sortDate\":1572278400000,\"type\":\"积分兑礼\"},{\"date\":\"10/29/2019 06:34:07\",\"point\":\"-1积分\",\"sortDate\":1572278400000,\"type\":\"积分兑礼\"},{\"date\":\"10/29/2019 08:12:10\",\"point\":\"-1积分\",\"sortDate\":1572278400000,\"type\":\"积分兑礼\"},{\"date\":\"10/29/2019 08:16:23\",\"point\":\"-1积分\",\"sortDate\":1572278400000,\"type\":\"积分兑礼\"},{\"date\":\"10/29/2019 08:37:31\",\"point\":\"-1积分\",\"sortDate\":1572278400000,\"type\":\"积分兑礼\"},{\"date\":\"10/29/2019 12:43:11\",\"point\":\"-1积分\",\"sortDate\":1572278400000,\"type\":\"积分兑礼\"},{\"date\":\"10/30/2019 04:39:31\",\"point\":\"-1积分\",\"sortDate\":1572364800000,\"type\":\"积分兑礼\"},{\"date\":\"10/30/2019 10:46:16\",\"point\":\"-1积分\",\"sortDate\":1572364800000,\"type\":\"积分兑礼\"},{\"accrualedValue\":100,\"date\":\"07/19/2019 09:27:11\",\"mainBody\":1,\"point\":\"+100积分\",\"sortDate\":1563465600000,\"type\":\"大单会员 \",\"usedValue\":100},{\"date\":\"02/19/2020 00:29:37\",\"point\":\"-100积分\",\"sortDate\":1582041600000,\"type\":\"退单\"},{\"accrualedValue\":300,\"date\":\"07/19/2019 09:28:27\",\"mainBody\":1,\"point\":\"+300积分\",\"sortDate\":1563465600000,\"type\":\"京东退单加折扣 \",\"usedValue\":0},{\"accrualedValue\":300,\"date\":\"07/19/2019 09:28:36\",\"mainBody\":1,\"point\":\"+300积分\",\"sortDate\":1563465600000,\"type\":\"升级奖励积分\",\"usedValue\":102},{\"date\":\"02/19/2020 00:29:37\",\"point\":\"-102积分\",\"sortDate\":1582041600000,\"type\":\"退单\"},{\"accrualedValue\":600,\"date\":\"07/19/2019 09:28:27\",\"mainBody\":1,\"point\":\"+600积分\",\"sortDate\":1563465600000,\"type\":\"大单会员 \",\"usedValue\":0},{\"accrualedValue\":610,\"date\":\"07/19/2019 11:06:37\",\"mainBody\":1,\"point\":\"+610积分\",\"sortDate\":1563465600000,\"type\":\"大单会员 \",\"usedValue\":0},{\"accrualedValue\":650,\"date\":\"02/18/2020 16:18:54\",\"mainBody\":1,\"point\":\"+650积分\",\"sortDate\":1581955200000,\"type\":\"大单会员 \",\"usedValue\":0},{\"accrualedValue\":540,\"date\":\"09/15/2017 09:52:48\",\"mainBody\":1,\"point\":\"+540积分\",\"sortDate\":1505404800000,\"type\":\"\",\"usedValue\":540},{\"date\":\"11/03/2018 02:22:34\",\"point\":\"-540积分\",\"sortDate\":1541174400000,\"type\":\"积分到期\"}]";
//
//		String str1 = x.replaceAll("\\\\","");
//
//		System.out.println(str1);
//
//		List<PointChangeLog> logList =  JSONArray.parseArray(str1, PointChangeLog.class);
//
//
//
//		String s =  JSON.toJSONString(logList);
//
//		System.out.println(s);
//
//
//		Calendar calendar = Calendar.getInstance();
//		calendar.add(Calendar.MONTH, -3);
//
//		Date oldTime = calendar.getTime();
//
//		List<PointChangeLog> collect = logList.stream().filter(u -> u.getMainBody()!=null&&oldTime.getTime() < u.getSortDate().getTime()&& u.getAccrualedValue() - u.getUsedValue() > 0).map(temp -> {
//
//			Integer diff = temp.getAccrualedValue() - temp.getUsedValue();
//			temp.setPoint(diff + "积分");
//
//			return temp;
//		}).collect(Collectors.toList());
//
//
//		System.out.println(JSON.toJSONString(collect));
//
//		System.out.println(collect.size());
//
//	}


}
